package ejb;

import db.facadelocals.GPFacadeLocal;
import db.facadelocals.PatientFacadeLocal;
import details.AddressDetails;
import details.GPDetails;
import details.PatientDetails;
import exceptions.GPNotFoundException;
import exceptions.PatientNotFoundException;
import exceptions.UsernameAlreadyExistsException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author jeroen
 */
@Stateless
public class PatientBean implements PatientLocal {

    @EJB
    private PatientFacadeLocal patientFacade;

    @EJB
    private GPFacadeLocal gpFacade;

    public void registerPatient(String username, String password, String ssn) throws UsernameAlreadyExistsException {
        patientFacade.registerPatient(username, password, ssn);
    }

    public void registerPatient(PatientDetails patientDetails) throws UsernameAlreadyExistsException {
        patientFacade.registerPatient(patientDetails.getUsername(), patientDetails.getPassword(), patientDetails.getSsn(),
                            patientDetails.getName(), patientDetails.getSurname(), patientDetails.getBloodgroup(),
                            patientDetails.getBirthday(), patientDetails.getAddress());
    }

    public PatientDetails getPatient(String username) throws PatientNotFoundException {
        return patientFacade.getPatient(username);
    }

    public void setPatientPassword(String username, String password) throws PatientNotFoundException {
        patientFacade.setPassword(username, password);
    }

    public void setPatientSsn(String username, String ssn) throws PatientNotFoundException {
        patientFacade.setSsn(username, ssn);
    }

    public void setPatientName(String username, String name) throws PatientNotFoundException {
        patientFacade.setName(username, name);
    }

    public void setPatientSurname(String username, String surname) throws PatientNotFoundException {
        patientFacade.setSurname(username, surname);
    }

    public void setPatientBloodgroup(String username, String bloodgroup) throws PatientNotFoundException {
        patientFacade.setBloodgroup(username, bloodgroup);
    }

    public void setPatientBirthday(String username, Calendar birthday) throws PatientNotFoundException {
        patientFacade.setBirthday(username, birthday);
    }

    public void setPatientAddress(String username, AddressDetails address) throws PatientNotFoundException {
        patientFacade.setAddress(username, address.getA1(), address.getA2(), address.getCity(),
                               address.getPostalCode(), address.getCountry(), address.getPhone(),
                               address.getEmail());
    }

    public List<GPDetails> getAllGPsForPatient(String usernamePatient) throws PatientNotFoundException {
        List<GPDetails> gps = new ArrayList<GPDetails>();

        for(String s : patientFacade.getGPs(usernamePatient)){
            try {
                gps.add(gpFacade.getGP(s));
            } catch (GPNotFoundException ex) {
                Logger.getLogger(PatientBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return gps;
    }

    public List<PatientDetails> getAllPatients() {
        return patientFacade.getAllPatients();
    }

    public void setNextConsult(String username, Date nextConsult) throws PatientNotFoundException {
        patientFacade.setNextConsult(username, nextConsult);
    }

    public void setLastConsult(String username, Date lastConsult) throws PatientNotFoundException {
        patientFacade.setLastConsult(username, lastConsult);
    }

    public void addGPToPatient(String usernamePatient, String usernameGP) throws PatientNotFoundException, GPNotFoundException {
        if(gpFacade.existGP(usernameGP)){
            gpFacade.addPatientToGP(usernameGP, usernamePatient);
        } else {
            throw new GPNotFoundException(usernameGP);
        }
    }

    public void removeGPFromPatient(String usernamePatient, String usernameGP) throws PatientNotFoundException, GPNotFoundException {
        if(gpFacade.existGP(usernameGP)){
            gpFacade.removePatientFromGP(usernameGP, usernamePatient);
        } else {
            throw new GPNotFoundException(usernameGP);
        }
    }


}
